In this case, we've factored out much of the code in the a base class called AbstractTemplateMethod. We have one concrete implementation of this class called Example2. The driver class, JdbcExample, now uses Exmaple2 to:
Install the schema
Insert some rows
Execute a query
Class Diagram
The Source
AbstractTemplateMethod.java
01: packageaab.valtech.jug.templatemethod;
02:
03: importjava.sql.Connection;
04: importjava.sql.PreparedStatement;
05: importjava.sql.ResultSet;
06: importjava.sql.SQLException;
07: importjava.sql.Statement;
08:
09: importorg.springframework.jdbc.datasource.DriverManagerDataSource;10:
11: importcom.valtech.util.DatabaseUtil;12:
13: publicabstractclass AbstractTemplateMethod {14: privatefinal DriverManagerDataSource dataSource;15:
16: public AbstractTemplateMethod(){17: dataSource = new DriverManagerDataSource();18: dataSource.setDriverClassName("org.hsqldb.jdbcDriver");19: dataSource.setUrl("jdbc:hsqldb:mem:mem:aname");20: dataSource.setUsername("sa");21: dataSource.setPassword("");22: }23:
24: publicvoid installSchema()throwsSQLException{25: Connection c = null;// NOPMD by brett.schuchert on 7/11/06 11:27 PM26: Statement s = null;// NOPMD by brett.schuchert on 7/11/06 11:27 PM27:
28: try{29: c = dataSource.getConnection();30: s = c.createStatement();31: installSchemaImpl(s);32: }finally{33: DatabaseUtil.close(s);34: DatabaseUtil.close(c);35: }36: }37:
38: protectedabstractvoid installSchemaImpl(Statement s)throwsSQLException;39:
40: publicvoid populateTables()throwsSQLException{41: Connection c = null;// NOPMD by brett.schuchert on 7/11/06 11:27 PM42: PreparedStatement ps = null;43:
44: try{45: c = dataSource.getConnection();46: ps = c.prepareStatement(getInsertStatement());47: populateTablesImpl(ps);48: }finally{49: DatabaseUtil.close(ps);50: DatabaseUtil.close(c);51: }52: }53:
54: protectedabstractvoid populateTablesImpl(PreparedStatement ps)throwsSQLException;55:
56: protectedabstractString getInsertStatement();57:
58: publicvoid performSearch()throwsSQLException{59: Connection c = null;// NOPMD by brett.schuchert on 7/11/06 11:27 PM60: PreparedStatement ps = null;61: ResultSet rs = null;// NOPMD by brett.schuchert on 7/11/06 11:27 PM62:
63: try{64: c = dataSource.getConnection();65: ps = c.prepareStatement(getSearchStatement());66: performSearchImpl(ps);67: }finally{68: DatabaseUtil.close(rs);69: DatabaseUtil.close(ps);70: DatabaseUtil.close(c);71: }72: }73:
74: protectedabstractvoid performSearchImpl(PreparedStatement ps)throwsSQLException;75:
76: protectedabstractString getSearchStatement();77: }
JDBC Using the Template Method Pattern
This version uses the GoF design pattern Template Method. For more details, take a look at this Template Method Pattern Example.In this case, we've factored out much of the code in the a base class called AbstractTemplateMethod. We have one concrete implementation of this class called Example2. The driver class, JdbcExample, now uses Exmaple2 to:
Class Diagram
The Source
AbstractTemplateMethod.java
Interesting Lines
Example2.java
Interesting Lines
JdbcExample.java
Interesting Lines
<--Back Next-->